package downstream

import (
	"context"
	"github.com/smartwalle/alipay/v3"
	"pay/api/bilinadmin"
	"pay/api/bmember"
	v1 "pay/api/open"
	"pay/api/wxpay"
	"pay/internal/conf"
	"pay/internal/model"
	"pay/pkg/ecpay"
	"pay/pkg/env"
)

type Downstream struct {
	// 第三方平台
	AlipayClient    *alipay.Client
	BytedanceClient *ecpay.Client
	// 下游系统
	adminClient      bilinadmin.BilinAdminClient
	employeeClient   bilinadmin.BilinEmployeeClient
	permissionClient bilinadmin.BilinPermissionClient
	wxpayClient      wxpay.WxpayClient
	memberClient     bmember.AssetClient
	openClient       v1.OpenServiceClient
}

func New(ctx context.Context, config *conf.Downstream, envType env.Type) (downstream *Downstream) {
	downstream = &Downstream{}
	_ = downstream.initAlipayClient(envType)

	// 品牌
	downstream.adminClient, _ = newAdminClient(ctx, config.AdminAddress)
	downstream.employeeClient, _ = newEmployeeClient(ctx, config.AdminAddress)
	downstream.permissionClient, _ = newPermissionClient(ctx, config.AdminAddress)
	// 电商收付通
	downstream.wxpayClient, _ = newWxpayClient(ctx, config.WxpayAddress)
	// 会员
	downstream.memberClient, _ = newMemberClient(ctx, config.MemberAddress)
	// 授权管理
	downstream.openClient, _ = newOpenClient(ctx, config.OpenAddress)
	// 第三方平台
	downstream.BytedanceClient = ecpay.NewClient(
		model.BootInfo.ByteDanceThirdPartyId, model.BootInfo.ByteDanceAppSecret)
	return
}
